﻿<%@ WebHandler Language="C#" Class="banhezhanmingxi" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using ServiceStack.Redis;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class banhezhanmingxi : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string zhanDian = context.Request.QueryString["zhandian"];
            string kaiShiShiJian = context.Request.QueryString["kaishishijian"];
            string jieShuShiJian = context.Request.QueryString["jieshushijian"];
            if (string.IsNullOrEmpty(zhanDian))
            {
                context.Response.Write("[]");
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                string createCondition = string.Empty;
                if(zhanDian!="—全部—")
                {
                    createCondition+= m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                {
                    createCondition += m_DB.CreateAndCondition("shijian", DataValueCommonType.日期时间,kaiShiShiJian,jieShuShiJian, SearchConditionType.between);
                }
                Dictionary<string, string> orderBy = new Dictionary<string, string>();
                orderBy.Add("shijian", "desc");
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_banhezhan", "*" , null, createCondition, null, orderBy);
                DataTable DtCount = m_DB.GetDataTableByCondition("tbl_banhezhan", "sum(gl1) as gl1,sum(gl2) as gl2,sum(gl3) as gl3,sum(gl4) as gl4,sum(gl5) as gl5,sum(fl) as fl,sum(shui) as shui",null, createCondition,null,null);
                if (Dt.Rows.Count > 0)
                {
                    DataRow newRow = Dt.NewRow();
                    newRow["zhandian"] = "合计：";
                    newRow["gl1"] = DtCount.Rows[0]["gl1"];
                    newRow["gl2"] = DtCount.Rows[0]["gl2"];
                    newRow["gl3"] = DtCount.Rows[0]["gl3"];
                    newRow["gl4"] = DtCount.Rows[0]["gl4"];
                    newRow["gl5"] = DtCount.Rows[0]["gl5"];
                    newRow["fl"] = DtCount.Rows[0]["fl"];
                    newRow["shui"] = DtCount.Rows[0]["shui"];
                    Dt.Rows.Add(newRow);
                    string dtJson = JsonConvert.SerializeObject(Dt);
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null)
                        {

                            redis.SetValue(key, dtJson, TimeSpan.FromMinutes(10.0));
                        }
                    }
                    context.Response.Write(dtJson);
                }
                else
                {
                    context.Response.Write("[]");
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("查询拌合站明细异常。{0}", err.Message));
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}